package close.kenny.shell.view
{
	import close.kenny.lib.kwindows.shortcut.ShortCutItem;
	import close.kenny.lib.kwindows.shortcut.events.ShortCutItemEvent;
	import close.kenny.lib.kwindows.taskbar.events.KTaskButtonEvent;
	import close.kenny.lib.kwindows.taskbar.taskbutton.KTaskButton;
	import close.kenny.lib.kwindows.window.KWindow;
	import close.kenny.lib.kwindows.window.events.KWindowEvent;
	import close.kenny.shell.ShellFacade;
	import close.kenny.shell.view.ui.Login;
	import close.kenny.shell.view.ui.WindowsContainer;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.IVisualElementContainer;
	import mx.events.FlexEvent;
	import mx.utils.ArrayUtil;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class WindowsContainerMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "WindowsContainerMediator";
		public function WindowsContainerMediator(viewComponent:WindowsContainer)
		{
			super(NAME, viewComponent);
		}
		private function get windowsContainer():WindowsContainer
		{
			return viewComponent as WindowsContainer;
		}
		override public function onRegister():void
		{
			windowsContainer.taskbar.minimizeAllWindow.addEventListener(MouseEvent.CLICK,minimizeAllWindow);
			windowsContainer.shortCut.addEventListener(ShortCutItemEvent.DOCK_ITEM_CLICK,shortCutClickhanle);
		}
		override public function onRemove():void
		{
			windowsContainer.taskbar.minimizeAllWindow.removeEventListener(MouseEvent.CLICK,minimizeAllWindow);
			windowsContainer.shortCut.removeEventListener(ShortCutItemEvent.DOCK_ITEM_CLICK,shortCutClickhanle);
		}
		override public function listNotificationInterests():Array
		{
			return [ShellFacade.ADDWINDOW,ShellFacade.SHORTCUTINI,];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ShellFacade.ADDWINDOW:
					var win:KWindow=note.getBody() as KWindow;
						addWindow(win,win.title,win.name);
					break;
				case ShellFacade.SHORTCUTINI:
					var xmlc:XMLListCollection=note.getBody() as XMLListCollection;
					windowsContainer.shortCut.dataProvider=xmlc;
					break;
			}
		}
		[Bindable]
		public var windowList:Array=new Array();
		[Bindable]
		public var taskButtonList:Array=new Array();
		public function addWindow(win:KWindow,winTitle:String,winName:String):void
		{
			if(verify(win,winTitle,winName)){
				windowList.push(win);
				windowsContainer.winZone.addElement(win);
				bringWindowToTop(win);
				addWindowListeners(win);
				win.x=this.windowList.length * 15 + 200;
				win.y=this.windowList.length * 15;
					if ((win.x + 400) > this.windowsContainer.winZone.width){
						win.x=40;
				}
					if ((win.y + 250) > this.windowsContainer.winZone.height){
						win.y=40;
				}
			
				var taskButton:KTaskButton=new KTaskButton(win);
				taskButton.label=win.title;
				taskButton.name=win.name;
				taskButtonList.push(taskButton);
				windowsContainer.taskbar.addElement(taskButton);
				taskButton.addEventListener(MouseEvent.CLICK, this.taskbarButtonHandle);
				taskButton.addEventListener(KTaskButtonEvent.CLOSE,this.taskbarButtonCloseHandle);
			}
		}
		private function verify(win:KWindow,winTitle:String,winName:String):Boolean
		{
			if(win.title==winTitle&&win.name==winName){
				return true;
			}
			else{
				return false;
			}
		}
		public function removeWindow(win:KWindow):void
		{
			removeWindowListeners(win);
			windowsContainer.winZone.removeElement(win);
			var index:int=ArrayUtil.getItemIndex(win, this.windowList);
			windowList.splice(index, 1);
			for each (var btn:KTaskButton in taskButtonList)
			{
				if (btn.name == win.name)
				{
					var index2:int=ArrayUtil.getItemIndex(btn, this.taskButtonList);
					taskButtonList.splice(index2, 1);
					IVisualElementContainer(btn.parent).removeElement(btn);
					btn.removeEventListener(MouseEvent.CLICK, taskbarButtonHandle);
					break;
				}
			}
		}
		
		public function bringWindowToTop(window:KWindow):void
		{
			for each (var win:KWindow in windowList)
			{
				if (win != window && win.hasFocus)
				{
					win.hasFocus=false;
				}
				if (win == window && !win.hasFocus)
				{
					win.hasFocus=true;
					win.visible=true;
				}
			}
		}
		private function minimizeAllWindow(event:MouseEvent):void
		{
			for each (var win:KWindow in windowList)
			{
				windowMinimize(win);
			}
		}
		private function windowMinimize(window:KWindow):void
		{
			window.hasFocus=false;
			window.visible=false;
			for each (var btn:KTaskButton in taskButtonList)
			{
				if (btn.name == window.name)
				{
					btn.selected=false;
					break;
				}
			}
			
		}
		
		private function MouseDownwindow(event:MouseEvent):void
		{
			bringWindowToTop(event.currentTarget as KWindow);
		}
		
		private function addWindowListeners(window:KWindow):void
		{
			window.addEventListener(KWindowEvent.CLOSE, windowListenerFunctions);
			window.addEventListener(KWindowEvent.MINIMIZE, windowListenerFunctions);
			window.addEventListener(KWindowEvent.FOCUS_START, windowListenerFunctions);
			window.addEventListener(KWindowEvent.FOCUS_END, windowListenerFunctions);
			window.addEventListener(MouseEvent.MOUSE_DOWN, MouseDownwindow);
		}
		
		private function removeWindowListeners(window:KWindow):void
		{
			window.removeEventListener(KWindowEvent.CLOSE, windowListenerFunctions);
			window.removeEventListener(KWindowEvent.MINIMIZE, windowListenerFunctions);
			window.removeEventListener(KWindowEvent.FOCUS_START, windowListenerFunctions);
			window.removeEventListener(KWindowEvent.FOCUS_END, windowListenerFunctions);
			window.removeEventListener(MouseEvent.MOUSE_DOWN, MouseDownwindow);
		}
		
		private function windowListenerFunctions(event:KWindowEvent):void
		{
			switch (event.type)
			{
				case KWindowEvent.CLOSE:
					removeWindow(event.target as KWindow);
					break;
				case KWindowEvent.MINIMIZE:
					windowMinimize(event.target as KWindow);
					break;
				case KWindowEvent.FOCUS_START:
					taskbarButtonWhenFoucsStart((event.target as KWindow).name);
					break;
				case KWindowEvent.FOCUS_END:
					taskbarButtonWhenFoucsEnd((event.target as KWindow).name);
					break;
			}
		}
		
		private function taskbarButtonWhenFoucsStart(windowName:String):void
		{
			for each (var btn:KTaskButton in taskButtonList)
			{
				if (btn.name == windowName)
				{
					btn.selected=true;
					break;
				}
			}
		}
		
		private function taskbarButtonWhenFoucsEnd(windowName:String):void
		{
			for each (var btn:KTaskButton in taskButtonList)
			{
				if (btn.name == windowName)
				{
					btn.selected=false;
					break;
				}
			}
		}
		
		private function taskbarButtonHandle(event:MouseEvent):void
		{
			if ((event.target as KTaskButton).selected == true)
			{
				for each (var win:KWindow in windowList)
				{
					if (win.name == (event.target as KTaskButton).name)
					{
						bringWindowToTop(win);
						break;
					}
				}
			}
			if ((event.target as KTaskButton).selected != true)
			{
				for each (var win1:KWindow in windowList)
				{
					if (win1.name == (event.target as KTaskButton).name)
					{
						windowMinimize(win1);
						break;
					}
				}
			}
		}
		private function taskbarButtonCloseHandle(event:close.kenny.lib.kwindows.taskbar.events.KTaskButtonEvent):void
		{
			for each (var win2:KWindow in windowList)
			{
				if(win2.name==(event.target as KTaskButton).name)
				{
					removeWindow(win2);
					break;
				}
			}
		}
		
		private function shortCutClickhanle(event:ShortCutItemEvent):void
		{
			
			var wink:KWindow=new KWindow();
			wink.title=event.render.labelString;
			wink.name=event.render.urlString;
			sendNotification(ShellFacade.ADDWINDOW,wink);
		}
	}
}